import Pacman from './Pacman.js';
import MovingDirectioin from './MovingDirection.js';

export default class TileMap {
    constructor(tileSize) {
        this.tileSize = tileSize;
        this.yellowDot = new Image()
        this.yellowDot.src = '../images/yellowDot.png';

        this.wall = new Image()
        this.wall.src = '../images/wall.png';
    }


    //1-wall
    //0-dots
    //4-pacman
    map = [
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [1, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 1],
        [1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1],
        [1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1],
        [1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1],
        [1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1],
        [1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1],
        [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
        [1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1],
        [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
    ]

    draw(ctx) {
        for (let row = 0; row < this.map.length; row++) {
            for (let column = 0; column < this.map[row].length; column++) {
                let tile = this.map[row][column];
                if (tile === 1) {
                    this.#drawWall(ctx, column, row, this.tileSize)
                } else if (tile === 0) {
                    this.#drawDot(ctx, column, row, this.tileSize)
                } 
                ctx.strokeStyle = 'yellow';
                ctx.strokeRect(column * this.tileSize, row * this.tileSize, this.tileSize, this.tileSize)
            }
        }
    }

    getPacman(velocity) {
        for (let row = 0; row < this.map.length; row++) {
            for (let column = 0; column < this.map[row].length; column++) { 
                let tile = this.map[row][column];
                if (tile === 4) {
                    this.map[row][column] =0
                    return new Pacman(column*this.tileSize,row*this.tileSize,this.tileSize,velocity,this)
                }
            }
        }
    }

    //私有方法
    //豆豆
    #drawDot(ctx, column, row, size) {
        ctx.drawImage(this.yellowDot, column * this.tileSize, row * this.tileSize, size, size)
    }
    //瓷砖
    #drawWall(ctx, column, row, size) {
        ctx.drawImage(this.wall, column * this.tileSize, row * this.tileSize, size, size)
    }

    setCanvasSize(canvas) {
        canvas.width = this.map[0].length * this.tileSize;
        canvas.height = this.map.length * this.tileSize;
    }

    didCollideWithEnvironment(x, y, deriction) {

        //
        if (deriction == null) {
            return;
        }
        
        if (Number.isInteger(x / this.tileSize) && Number.isInteger(y / this.tileSize)) {
            let column = 0
            let row = 0
            let nextColumn = 0
            let nextRow = 0

            switch (deriction) {
                case MovingDirectioin.right:
                    nextColumn = x + this.tileSize
                    column = nextColumn / this.tileSize
                    row = y / this.tileSize
                    break;
                case MovingDirectioin.left:
                    nextColumn = x - this.tileSize
                    column = nextColumn / this.tileSize
                    row = y / this.tileSize
                    break;
                case MovingDirectioin.up:
                    nextRow = y - this.tileSize
                    column = x / this.tileSize
                    row = nextRow / this.tileSize
                    break;
                case MovingDirectioin.down:
                    nextRow = y + this.tileSize
                    column = x / this.tileSize
                    row = nextRow / this.tileSize
                    break;
                
            }
            const tile = this.map[row][column];
            if (tile === 1) {
                return true;
            } 
        }
        return false;
    }
}